package com.larry.mr.index;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @author larry
 * @Title: map-reduce
 * @Package com.larry.mr.index
 * @Description: TODO
 * @date 2022/5/1716:21
 */
public class OneIndexMapper extends Mapper<LongWritable, Text,Text, IntWritable> {

    String name;
    Text k = new Text();
    IntWritable v = new IntWritable(1);

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        //获取文件名称
        FileSplit inputSplit = (FileSplit) context.getInputSplit();
        name = inputSplit.getPath().getName();

    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String[] fields = value.toString().split(" ");
        for (String field : fields) {
            k.set(field + "--" + name);
            context.write(k,v);
        }

    }
}